package com.enqualcomm.kids.view.fencing;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;

import java.util.concurrent.TimeUnit;

import rx.Observable;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;

/**
 * Created by yinzhiqun on 2016/3/7.
 * 地图和电子围栏的父控件，处理触摸事件
 */
public class FencingViewLayout extends FrameLayout {
    private FencingView fencingView;

    public FencingViewLayout(Context context) {
        this(context, null);
    }

    public FencingViewLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FencingViewLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void addView(View child) {
        super.addView(child);
        if (child instanceof FencingView){
            this.fencingView = (FencingView) child;
        }
    }

    @Override
    public void addView(View child, int index) {
        super.addView(child, index);
        if (child instanceof FencingView){
            this.fencingView = (FencingView) child;
        }
    }

    @Override
    public void addView(View child, int width, int height) {
        super.addView(child, width, height);
        if (child instanceof FencingView){
            this.fencingView = (FencingView) child;
        }
    }

    @Override
    public void addView(View child, ViewGroup.LayoutParams params) {
        super.addView(child, params);
        if (child instanceof FencingView){
            this.fencingView = (FencingView) child;
        }
    }

    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        super.addView(child, index, params);
        if (child instanceof FencingView){
            this.fencingView = (FencingView) child;
        }
    }

    boolean editMode;
    Subscription subscription;
    float recentX;
    float recentY;
    boolean canceled;

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        if (fencingView!=null){
            switch (event.getAction()){
                case MotionEvent.ACTION_DOWN:
                    canceled = false;
                    recentX = event.getX();
                    recentY = event.getY();
                    //判断是否在电子围栏范围内
                    Rect rect = new Rect();
                    fencingView.getGlobalVisibleRect(rect);
                    if (rect.contains((int)event.getRawX(),(int)event.getRawY())){
                        subscription= Observable.timer(300, TimeUnit.MILLISECONDS).observeOn(AndroidSchedulers.mainThread())
                                .subscribe(new Action1<Long>() {
                                    @Override
                                    public void call(Long aLong) {
                                        editMode = true;
                                        fencingView.setEditMode(true);
                                    }
                                });
                    }
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (canceled){
                        break;
                    }
                    if (editMode){
                        float currentX = event.getX();
                        float currentY = event.getY();
                        int middleX = getWidth()/2;
                        int middleY = getHeight()/2;

                        if (recentX>middleX){
                            fencingView.zoomX(currentX-recentX);
                        }else{
                            fencingView.zoomX(recentX-currentX);
                        }

                        if (recentY>middleY){
                            fencingView.zoomY(currentY - recentY);
                        }else{
                            fencingView.zoomY(recentY - currentY);
                        }

                        recentX = currentX;
                        recentY = currentY;
                        return false;//不需要地图继续响应
                    }else{
                        //目前还不在编辑模式,手指落下点不在范围内，或者时间未到
                        float x = event.getX();
                        float y = event.getY();

                        if (Math.abs(x-recentX)>10||Math.abs(y-recentY)>10){
                            canceled = true;
                            if (subscription!=null&&!subscription.isUnsubscribed()){
                                subscription.unsubscribe();
                            }
                        }
                    }
                    break;
                case MotionEvent.ACTION_UP:
                case MotionEvent.ACTION_CANCEL:
                    if (subscription!=null&&!subscription.isUnsubscribed()){
                        subscription.unsubscribe();
                    }
                    if (editMode){
                        editMode = false;
                        fencingView.setEditMode(false);
                    }
                    break;
            }
        }
        return super.dispatchTouchEvent(event);
    }
}
